package pt.ipleiria.estg.es2.byinvitationonly;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import pt.ipleiria.estg.es2.byinvitationonly.modelo.Contact;


public class MainActivity extends Activity {

    public static boolean imHere;
    private Contact contact;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imHere = false;

        requestUserContact();

    }

    private void requestUserContact() {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);

        String name = sp.getString(getString(R.string.pref_key_name), "");
        String email = sp.getString(getString(R.string.pref_key_email), "");

        this.contact = new Contact(name, email);

    }

    private void setImHere(boolean state, final MenuItem item) {
        if (state) {
            if (contact != null && !contact.getName().isEmpty() && !contact.getEmail().isEmpty()) {

                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle(R.string.general_procede_question);
                builder.setPositiveButton(R.string.general_yes, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (temRede()) {
                            imHere = true;
                            item.setIcon(R.drawable.ic_action_not_alone);
                            Intent intent = new Intent(getApplicationContext(), WhoIsHereActivity.class);
                            startActivity(intent);
                        } else {
                            Toast.makeText(getApplicationContext(), R.string.main_activity_no_network, Toast.LENGTH_LONG).show();
                        }

                    }
                }).setNegativeButton(R.string.general_no, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

                builder.create().show();

            } else {
                Toast.makeText(this, R.string.main_activity_no_contact_info, Toast.LENGTH_LONG).show();
            }
        } else {
            imHere = false;
            item.setIcon(R.drawable.ic_action_alone);
        }
    }

    private boolean temRede() {
        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_main_im_here) {
            setImHere(!(imHere), item);
        }

        return super.onOptionsItemSelected(item);
    }
}
